package cn.songtx.iot.controller;

import cn.songtx.iot.pojo.Result;
import cn.songtx.iot.pojo.SysFamily;
import cn.songtx.iot.pojo.SysRoom;
import cn.songtx.iot.service.ISysFamilyService;
import cn.songtx.iot.service.ISysRoomService;
import cn.songtx.iot.vo.FamilyRoomVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("room")
@RequiredArgsConstructor
public class SysRoomController {
    private final ISysRoomService roomService;
    private final ISysFamilyService familyService;

    /**
     * 房间管理:创建房间
     * 创建时,检测数据完整性
     */
    @RequestMapping(value = "/save", method = RequestMethod.GET)
    public Result roomSave(String roomName, String information, Long familyId) {
        if (roomName != null && information != null && familyId != null) {
            SysRoom sysRoom = new SysRoom();
            sysRoom.setRoomName(roomName);
            sysRoom.setInformation(information);
            sysRoom.setFamilyId(familyId);
            sysRoom.setCreateTime(LocalDateTime.now());
            boolean saved = roomService.save(sysRoom);
            return Result.success(saved ? "创建成功" : "创建失败");
        }
        return Result.success("数据不全,无法创建");
    }

    /**
     * 房间管理:逻辑删除
     * 删除房间后,所有下游的equipment都无法查询到
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.DELETE)
    public Result roomDeleteById(@PathVariable("id") Long id) {
        boolean removed = roomService.removeById(id);
        return Result.success(removed ? "逻辑删除成功" : "逻辑删除失败");
    }

    /**
     * 房间管理:根据id,可以修改家庭名称、房间名称、描述
     */
    @RequestMapping(value = "/updateRoom", method = RequestMethod.GET)
    public Result roomUpdate(Long id, Long familyId, String roomName, String information) {
        if (id != null) {
            SysRoom sysRoom = new SysRoom();
            sysRoom.setId(id);
            if (familyId != null) {
                sysRoom.setFamilyId(familyId);
            }
            if (roomName != null) {
                sysRoom.setRoomName(roomName);
            }
            if (information != null) {
                sysRoom.setInformation(information);
            }
            sysRoom.setUpdateTime(LocalDateTime.now());
            boolean updated = roomService.updateById(sysRoom);
            return Result.success(updated ? "修改成功" : "修改失败");
        }
        return Result.success("主键不能为空");
    }

    /**
     * 房间管理:根据user_id,查询所有的room数据,需要分两次查询两张表,此方法返回的数据非嵌套数组对象
     * 1.根据 sys_user_id 查询 sys_family 表,返回 List<family_ids>
     * 2.根据 List<family_ids> 查询 sys_room 表,返回rooms
     */
    @RequestMapping(value = "/listRoomByUserId", method = RequestMethod.GET)
    public Result roomListByUserId(Long sysUserId) {
        // 1.第一次查询,查询family表
        LambdaQueryWrapper<SysFamily> sysFamilyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysFamilyLambdaQueryWrapper.eq(SysFamily::getSysUserId, sysUserId);
        List<SysFamily> list = familyService.list(sysFamilyLambdaQueryWrapper);
        // 2.从返回的对象中获取id,stream的map操作
        List<Long> ids = list.stream().map(SysFamily::getId).collect(Collectors.toList());
        // 3.第二次查询,返回room数据
        LambdaQueryWrapper<SysRoom> roomLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLambdaQueryWrapper.in(SysRoom::getFamilyId, ids);
        List<SysRoom> listedByIds = roomService.list(roomLambdaQueryWrapper);
        return Result.success(listedByIds);
    }

    /**
     * 房间管理:根据user_id,查询,再Service完成嵌套FamilyRoomList对象数组,用于前端双层遍历循环
     *
     * @param sysUserId sys_user表中的主键id
     * @return 返回嵌套对象数组
     */
    @RequestMapping(value = "/listFamilyRoomListByUserId/{id}", method = RequestMethod.GET)
    public Result listFamilyRoomListByUserId(@PathVariable("id") Long sysUserId) {
        List<FamilyRoomVO> familyRoomVOS = roomService.selectFamilyRoomListByUserId(sysUserId);
        return Result.success(familyRoomVOS);
    }
}
